/**************************************************************
 *@ProjectName: blweibo
 *@FileName: MainTabActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.Toast;

import com.zhiweibo.models.StaticInfo;

/**
 * The Class MainTabActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener {

    /** The m host. */
    private TabHost mHost;

    /** The m radio buttons. */
    private RadioButton[] mRadioButtons;

    /** The mode. */
    public static int mode = 2;

    /** The tab select. */
    public static int tabSelect = 0;

    /** The message it. */
    private Intent messageIt;

    /** The handle. */
    public static Handler handle;

    /** The flag. */
    private boolean flag = false;

    /**
     * Instantiates a new main tab activity.
     */
    public MainTabActivity() {
        handle = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case 0:
                    if (flag) {
                        changeImage(false);
                        flag = false;
                    }
                    break;
                case 1:
                    if (!flag) {
                        changeImage(true);
                        flag = true;
                    }
                    break;
                case 2:
                    stopService(new Intent(MainTabActivity.this, WeiboEventService.class));
                    finish();
                    break;
                }
            }
        };
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        Display localDisplay = getWindowManager().getDefaultDisplay();
        int i = localDisplay.getWidth();
        int j = localDisplay.getHeight();
        WeiboApplication.setDisplay(i, j);
        if (TextUtils.isEmpty(StaticInfo.mUsername)) {
            startActivity(new Intent(this, SwitchUser.class));
            finish();
        } else {
            setContentView(R.layout.maintabs);
            Toast localToast = new Toast(this);
            View localView = LayoutInflater.from(this).inflate(R.layout.navigater, null);
            localToast.setView(localView);
            localToast.setGravity(17, 0, -1000);
            localToast.setDuration(1);
            localToast.show();
            mHost = this.getTabHost();
            mHost.addTab(mHost.newTabSpec("mblog_tab")
                    .setIndicator(getString(R.string.main_home), this.getResources().getDrawable(R.drawable.icon_1))
                    .setContent(new Intent(this, HomeListActivity.class)));

            messageIt = new Intent(this, MessageGroup.class);
            mHost.addTab(mHost.newTabSpec("message_tab")
                    .setIndicator(getString(R.string.main_message), this.getResources().getDrawable(R.drawable.icon_2))
                    .setContent(messageIt));
            mHost.addTab(mHost.newTabSpec("userinfo_tab")
                    .setIndicator(getString(R.string.user_info), this.getResources().getDrawable(R.drawable.icon_3))
                    .setContent(new Intent(this, MyInfoActivity.class)));
            mHost.addTab(mHost.newTabSpec("search_tab")
                    .setIndicator(getString(R.string.menu_search), this.getResources().getDrawable(R.drawable.icon_4))
                    .setContent(new Intent(this, SearchSquareActivity.class)));
            mHost.addTab(mHost.newTabSpec("more_tab")
                    .setIndicator(getString(R.string.more), this.getResources().getDrawable(R.drawable.icon_5))
                    .setContent(new Intent(this, MoreItemsActivity.class)));
            initRadios();
            if (SettingsPref.isAutoRemind(this)) {
                startService(new Intent(this, WeiboEventService.class));
            }
            startService(new Intent(this, PopupService.class));
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onNewIntent(android.content.Intent)
     */
    @Override
    protected void onNewIntent(Intent intent) {
        String strAct = intent.getAction();
        if (strAct != null) {
            if (strAct.equals("com.hbr.comment")) {
                tabSelect = 1;
                mode = 3;
            } else if (strAct.equals("com.hbr.at")) {
                tabSelect = 1;
                mode = 2;
            } else if (strAct.equals("com.hbr.message")) {
                tabSelect = 1;
                mode = 4;
            }
        }
        if (messageIt.hasExtra("mode")) {
            messageIt.removeExtra("mode");
        }
        messageIt.putExtra("mode", mode);
        mRadioButtons[tabSelect].toggle();
    }

    /**
     * Change image.
     * 
     * @param b
     *            the b
     */
    public void changeImage(boolean b) {
        RadioGroup localRadioGroup = (RadioGroup) findViewById(R.id.main_radio);
        RadioButton rb = (RadioButton) localRadioGroup.findViewWithTag("radio_button0");
        if (b) {
            rb.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.icon_1_new), null,
                    null);
        } else {
            rb.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.icon_1_n), null,
                    null);
        }
    }

    /**
     * Inits the radios.
     */
    private void initRadios() {
        RadioGroup localRadioGroup = (RadioGroup) findViewById(R.id.main_radio);
        this.mRadioButtons = new RadioButton[5];
        for (int j = 0; j < 5; j++) {
            mRadioButtons[j] = (RadioButton) localRadioGroup.findViewWithTag("radio_button" + j);
            mRadioButtons[j].setOnCheckedChangeListener(this);
        }
        mRadioButtons[0].toggle();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.ActivityGroup#onDestroy()
     */
    @Override
    protected void onDestroy() {
        stopService(new Intent(this, WeiboEventService.class));
        super.onDestroy();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged
     * (android.widget.CompoundButton, boolean)
     */
    @Override
    public void onCheckedChanged(CompoundButton cb, boolean b) {
        if (b) {
            for (int i = 0; i < 5; i++) {
                if (this.mRadioButtons[i].getText().equals(cb.getText())) {
                    this.mHost.setCurrentTab(i);
                }
            }
        }
    }

}